package com.jz.cash.client.api;

import com.jz.cash.client.model.User;
import com.jz.cash.client.query.PageBean;
import com.jz.cash.client.query.QueryBean;
import com.jz.cash.client.query.QueryBeanParsing;
import com.jz.cash.client.service.TableConfigService;
import com.jz.cash.client.service.TableTypeService;
import com.jz.cash.client.vo.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.LoggingPermission;

@RestController
@RequestMapping("table")
public class TableController {


    @Autowired
    private TableTypeService tableTypeService;

    @Autowired
    private TableConfigService tableConfigService;

    @GetMapping("category")
    public Json getCategory() {
        String oper = "get table category";

        User user = (User) SecurityUtils.getSubject().getPrincipal();

        List<TableCategoryVo> tableList = tableTypeService.getCategoryListByMerchantId(user.getMerchantId());

        return Json.succ(oper).data("tableTypeList", tableList);
    }

    @GetMapping("all")
    public Json getAllTable(QueryBean queryBean) {

        String oper = "find all table";
        User user = (User) SecurityUtils.getSubject().getPrincipal();

        PageBean tableList = tableConfigService.list(queryBean);

        return Json.succ(oper).data("tableList", tableList);
    }

    @PostMapping("reserve")
    public Json reserveTable(@RequestBody ReserveTableVo reserve) {
        String oper = "reserve table";
        User user = (User) SecurityUtils.getSubject().getPrincipal();

        tableConfigService.reserveTable(reserve, user);

        return Json.succ(oper);
    }

    @PostMapping("cancelReserve")
    public Json cancelReserve(Long tableId) {
        String oper = "cancel reserve table";
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        tableConfigService.cancelReserve(tableId, user);
        return Json.succ(oper);
    }

    @PostMapping("openStage")
    public Json openStage(@RequestBody OpenStageVo open) {
        String oper = "open stage";
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        tableConfigService.openStage(open, user);
        return Json.succ(oper);
    }

    @GetMapping("tableInfo")
    public Json tableInfo(QueryBean queryBean) {

        String oper = "get table info";
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        queryBean.setCurrentUser(user);

        TableInfoVo tableInfo = tableConfigService.tableInfo(queryBean);
        return Json.succ(oper).data("tableInfo", tableInfo);


    }
}
